package city.spring.modules.personnel.controller;

import city.spring.modules.personnel.entity.EmployeeJobEntity;
import city.spring.modules.personnel.service.EmployeeJobService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：职务
 *
 * @author pxm
 * @date 2020-06-28 17:17:30
 */
@RestController
@RequestMapping("/employee_job")
public class EmployeeJobController extends ApiController {
    private final EmployeeJobService employeeJobService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<EmployeeJobEntity, ?>> entityOrderFields;

    public EmployeeJobController(EmployeeJobService employeeJobService) {
        this.employeeJobService = employeeJobService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(EmployeeJobEntity::getId);
        entityOrderFields.add(EmployeeJobEntity::getName);
        entityOrderFields.add(EmployeeJobEntity::getOrgId);
        entityOrderFields.add(EmployeeJobEntity::getMarks);
        entityOrderFields.add(EmployeeJobEntity::getGmtCreate);
        entityOrderFields.add(EmployeeJobEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>职务</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, EmployeeJobEntity entity) {
        LambdaQueryChainWrapper<EmployeeJobEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>职务</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, EmployeeJobEntity entity) {
        LambdaQueryChainWrapper<EmployeeJobEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<EmployeeJobEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>职务</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(employeeJobService.getById(id));
    }

    /**
     * 添加一个 <strong>职务</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody EmployeeJobEntity entity) {
        employeeJobService.saveEmployeeJob(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>职务</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody EmployeeJobEntity entity) {
        entity.setId(id);
        employeeJobService.updateEmployeeJob(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>职务</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        employeeJobService.deleteEmployeeJob(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>职务</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        employeeJobService.deleteEmployeeJob(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<EmployeeJobEntity> buildLambdaQuery(EmployeeJobEntity entity) {
        LambdaQueryChainWrapper<EmployeeJobEntity> lambdaQuery = employeeJobService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), EmployeeJobEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), EmployeeJobEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOrgId()), EmployeeJobEntity::getOrgId, entity.getOrgId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMarks()), EmployeeJobEntity::getMarks, entity.getMarks());
        lambdaQuery.eq(entity.getGmtCreate() != null, EmployeeJobEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, EmployeeJobEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<EmployeeJobEntity> buildQuery(EmployeeJobEntity entity) {
        QueryWrapper<EmployeeJobEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getOrgId()), "org_id", entity.getOrgId());
        wrapper.like(StringUtils.isNotBlank(entity.getMarks()), "marks", entity.getMarks());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
